package com.code.leetcode.before50;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/23 18:42
 */
public class The56 {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        ArrayList<int[]> ints = new ArrayList<>();
        int i = 0;
        while (i < intervals.length) {
            int left = intervals[i][0];
            int right = intervals[i][1];
            while (i < intervals.length - 1 && right >= intervals[i + 1][0]) {
                i++;
                right = Math.max(intervals[i][1], right);
            }
            ints.add(new int[]{left, right});
            i++;
        }
        return ints.toArray(new int[0][]);
    }
}
